package com.tnsw.noxnox;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.coreutils.StaticConfig;
import com.tnsw.noxnox.util.SystemUiHider;
import com.tnsw.noxnox.util.SystemUiHider.OnVisibilityChangeListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class SelectModeActivity extends Activity implements OnVisibilityChangeListener, OnClickListener  {

	private String tag = "SelectModeActivity";
	private SystemUiHider uiHider;
	private Button btnSetTopBox, btnMobile;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_select_mode);
		
		//this.getWindow().getDecorView().getRootView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
		
		//View contentView = this.findViewById(R.id.SelectModeTitle);
		//this.uiHider = SystemUiHider.getInstance(this, contentView, SystemUiHider.FLAG_HIDE_NAVIGATION);
		//this.uiHider.setup();
		//this.uiHider.toggle();
		
		
		//this.uiHider.setOnVisibilityChangeListener(this);
		
		this.btnMobile = (Button) this.findViewById(R.id.btnMobile);
		this.btnMobile.setOnClickListener(this);
		
		this.btnSetTopBox = (Button) this.findViewById(R.id.btnSetTopBox);
		this.btnSetTopBox.setOnClickListener(this);
		
		this.detectSharedPreference();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_mode, menu);
		return true;
	}
	


	@Override
	public void onVisibilityChange(boolean visible) {
		Log.d(tag, "onVisibilityChange(b=" + visible + ")");
		if (visible == true) {
			//this.uiHider.toggle();
			this.getWindow().getDecorView().getRootView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
		}
		//this.uiHider.toggle();
		//this.uiHider.hide();
	}
	
	private void detectSharedPreference() {
		String currentMode = "";
		
		try {
			SharedPreferences settings = this.getSharedPreferences(tag, MODE_PRIVATE);
			currentMode = settings.getString("mode", "");			
		} catch (Exception e) {
			Log.e(tag, "detectSharedPreference() - " + e.toString(), e);
		}

		if (currentMode.equals("MOBILE")) {
			this.gotoMobile();
		} else if (currentMode.equals("SETTOPBOX")) {
			this.gotoSetTopBox();
		}
	}
	
	private void gotoMobile() {
		Intent intent = new Intent(this, com.tnsw.noxnox.MainActivity.class);
		this.startActivity(intent);
		
		//StaticConfig.LOG_URL = "http://172.16.50.192/PredictWeb/NetworkLog.aspx";
	}
	
	private void gotoSetTopBox() {
		Intent intent = new Intent(this, com.tnsw.noxnox.settopbox.SetTopBoxActivity.class);
		this.startActivity(intent);
		
		//StaticConfig.LOG_URL = "http://202.57.128.248/PredictWeb/NetworkLog.aspx";
		
		AsyncManager asyncManager = AsyncManager.getInstance();
		asyncManager.dumpDeviceInfo(this);
	}

	@Override
	public void onClick(View v) {
		SharedPreferences settings = this.getSharedPreferences(tag, MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		
		if (v == this.btnMobile) {
			editor.putString("mode", "MOBILE");
			editor.commit();
			this.gotoMobile();
		} else if (v == this.btnSetTopBox) {
			editor.putString("mode", "SETTOPBOX");
			editor.commit();
			this.gotoSetTopBox();
		}
		
	}

}
